package cn.hxlc.tie.demo.modular.ljt.equipment.controller;


import cn.hxlc.tie.demo.core.enums.StatusEnum;
import cn.hxlc.tie.demo.modular.ljt.equipment.entity.EquipmentGroup;
import cn.hxlc.tie.demo.modular.ljt.equipment.model.params.EquipmentGroupParam;
import cn.hxlc.tie.demo.modular.ljt.equipment.model.params.FacilityParam;
import cn.hxlc.tie.demo.modular.ljt.equipment.model.result.FacilityResult;
import cn.hxlc.tie.demo.modular.ljt.equipment.service.EquipmentCategoryService_lt;
import cn.hxlc.tie.demo.modular.ljt.equipment.service.EquipmentService_lt;
import cn.hxlc.tie.demo.modular.ljt.equipment.service.FacilityService_lt;
import cn.hxlc.tie.libs.scanner.annotation.GetResource;
import cn.hxlc.tie.libs.scanner.annotation.PostResource;
import cn.hxlc.tie.libs.scanner.stereotype.ApiResource;
import cn.hxlc.tie.model.web.response.ResponseData;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import java.util.Map;


@RestController
@ApiResource(name = "设备资料",path = "/equipment")
public class EquipmentController_lt {

    @Autowired
    private EquipmentService_lt equipmentService;

    @Autowired
    private EquipmentCategoryService_lt equipmentCategoryService;

    @Autowired
    private FacilityService_lt facilityService;

    @GetResource(name = "获取分组树列表", path="/queryGroupTree",requiredPermission = false)
    @ApiOperation(value = "分组树列表")
    public ResponseData queryGroupTree(){
        return ResponseData.success(equipmentService.queryTree());
    }


    @PostResource(name = "左树添加", path = "/add")
    @ApiOperation("新增")
    public ResponseData Add(@RequestBody EquipmentGroupParam param) {
        equipmentService.TreeCategoryAdd(param);
        return ResponseData.success(ResponseData.DEFAULT_ERROR_CODE, "新增成功", null);
    }

    @GetResource(name = "左树编辑", path = "/detail")
    @ApiOperation("编辑")
    public ResponseData detail(String fid) {
        EquipmentGroup select = equipmentService.selectById(fid);
        return ResponseData.success(select);
    }


    @PostResource(name = "修改", path = "/update")
    @ApiOperation("修改")
    public ResponseData Update(@RequestBody EquipmentGroupParam param) {
        equipmentCategoryService.updateEquipmentCategory(param);
        return ResponseData.success(ResponseData.DEFAULT_ERROR_CODE, "编辑成功", null);
    }


    @GetResource(name = "删除", path = "/delete")
    @ApiOperation("删除")
    public ResponseData Delete(String fid) {
        System.out.println("删除:" + fid);
        equipmentService.delete(fid);
        return ResponseData.success(ResponseData.DEFAULT_ERROR_CODE, "删除成功", null);
    }

    /**
     * ------  设备资料 ------
     */

    /**
     *  分页查询列表
     *  2021/07/13
     */
    @GetResource(name = "分页查询列表", path = "/queryListPage",requiredPermission = false)
    @ApiOperation(value = "分页查询列表",response = FacilityResult.class)
    public ResponseData QueryListPage(FacilityParam param) {
        return ResponseData.success(facilityService.findPageBySpec(param));
    }

    /**
     * facilityDetail
     */
    @GetResource(name = "设备详情", path = "/facilityDetail",requiredPermission = false)
    @ApiOperation(value = "设备详情",response = FacilityResult.class)
    public ResponseData facilityDetail(Integer fid) {
        return ResponseData.success(facilityService.selectById(fid));
    }

    @GetResource(name = "查询主设备", path = "/fisMain",requiredPermission = false)
    @ApiOperation(value = "查询主设备",response = FacilityResult.class)
    public ResponseData fisMain() {
        return ResponseData.success(facilityService.fisMain());
    }

    //addFacility
    @PostResource(name = "新增设备", path = "/addFacility")
    @ApiOperation("新增设备")
    public ResponseData addFacility(@RequestBody FacilityParam param) {
        facilityService.addFacility(param);
        return ResponseData.success(ResponseData.DEFAULT_ERROR_CODE, "新增成功", null);
    }

    //updateFacility
    @PostResource(name = "编辑设备", path = "/updateFacility")
    @ApiOperation("编辑设备")
    public ResponseData updateFacility(@RequestBody FacilityParam param) {
        facilityService.updateFacility(param);
        return ResponseData.success(ResponseData.DEFAULT_ERROR_CODE, "编辑成功", null);
    }

    //batchChangeState
    @PostResource(name = "启用禁用", path = "/batchChangeState",requiredPermission = false)
    @ApiOperation("启用禁用")
    public ResponseData batchChangeState(@RequestBody Map params) {
        JSONArray json = (JSONArray) params.get("fid");
        Object []fid = json.toArray();
        Integer fstatus = (Integer) params.get("fstatus");
        facilityService.batchChangeState(fid, StatusEnum.toEnum(fstatus));
        return ResponseData.success(ResponseData.DEFAULT_ERROR_CODE, "操作成功", null);
    }

    //batchDelete
    @PostResource(name = "删除", path = "/batchDelete",requiredPermission = false)
    @ApiOperation("删除")
    public ResponseData batchDelete(@RequestBody Map params) {
        JSONArray json = (JSONArray) params.get("fid");
        Object []fid = json.toArray();
        facilityService.batchDelete(fid);
        return ResponseData.success(ResponseData.DEFAULT_ERROR_CODE, "操作成功", null);
    }




}
